package com.itheima.advantagesup.controller;

import com.itheima.advantagesup.common.PageBean;
import com.itheima.advantagesup.common.PaginationVO;
import com.itheima.advantagesup.common.R;
import com.itheima.advantagesup.entity.SysRole;
import com.itheima.advantagesup.entity.addRole;
import com.itheima.advantagesup.service.SysRoleService;
import com.itheima.advantagesup.vo.RoleVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.management.relation.Role;
import java.util.List;

@RestController
@RequestMapping("/sys_role")
@Slf4j
public class RoleController {
    private final SysRoleService sysRoleService;

    public RoleController(SysRoleService sysRoleService) {
        this.sysRoleService = sysRoleService;
    }

    @GetMapping("/list")
    public R<PaginationVO> Page(@RequestParam(defaultValue = "1") String page,
                     @RequestParam(defaultValue = "10") String limit, String name) {
        //记录日志
        log.info("分页查询，参数：{},{},{}", page, limit, name);
        //调用业务层分页查询功能
        PaginationVO pages = sysRoleService.page(page, limit, name);
        //响应
        return R.success(pages);
    }

    @GetMapping("/allData")
    public R<List<SysRole>> allData() {
        //记录日志
        log.info("查询全部");
        //调用业务层分页查询功能
        List<SysRole> sysRoles = sysRoleService.allData();
        //响应
        return R.success(sysRoles);
    }

    @GetMapping("/del")
    public R<?> delRoleById(String id) {
        //记录日志
        log.info("根据id删除角色:{}", id);
        //调用业务层分页查询功能
        sysRoleService.delRoleById(id);
        //响应
        return R.success();
    }

    @GetMapping("/rowInfo")
    public R<RoleVO> rowInfoRoleById(String id) {
        //记录日志
        log.info("根据id获取角色详情:{}", id);
        RoleVO roleVO = sysRoleService.rowInfoRoleById(id);
        //响应
        return R.success(roleVO);
    }

    @PostMapping("/add")
    public R<?> addRole(@RequestBody addRole role) {
        //记录日志
        log.info("新增角色详情:{}", role);

        sysRoleService.addRole(role);
        return R.success("");
    }

    @PostMapping("/update")
    public R<?> updateRole(@RequestBody addRole role) {
        //记录日志
        log.info("修改角色详情:{}", role);
        sysRoleService.updateRole(role);
        return R.success("");
    }
}
